package com.eryos.android.holidaybudget;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.eryos.android.holidaybudget.beans.Budget;

/**
 * Project :  HolidayBudget
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/HolidayBudget/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *  
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class BudgetActivity extends RootActivity {

	private EditText budgetNameField;
	private EditText budgetTypeField;
	private EditText budgetAmountField;
	private Button 	 budgetCreateButton;
	
	@Override
    protected void initParam(){
		viewTitle = "Budget";
		hasTopBanner = true;
	}

    @Override
    protected void initUI(){
        Log.d(LOG_TAG, "initUI");

        /* Define the Layout */
        setContentView(R.layout.budgetlayout);
        
        showWaitingMessage();
    	 
        /* Retrieve UI elements */
    	budgetNameField		= (EditText) findViewById(R.id.budget_name_field);
    	budgetTypeField		= (EditText) findViewById(R.id.budget_type_field);
    	budgetAmountField	= (EditText) findViewById(R.id.budget_amout_field);
    	budgetCreateButton  = (Button) 	 findViewById(R.id.budget_create_button);

    }

    @Override
    protected void initHandlers(){
    	super.initHandlers();

    	Log.d(LOG_TAG, "initHandlers");
    	
    	budgetCreateButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Log.d(LOG_TAG, "Click Create Button");

            	/* Retrieve value from UI components */
                Budget budget = new Budget();
                budget.setName(budgetNameField.getText().toString());
                budget.setType(budgetTypeField.getText().toString());
                Float initialAmount = new Float(budgetAmountField.getText().toString());
    			budget.setInitialAmount(initialAmount);
  
                /* Store budget in DB */
                new BackgroundTask().execute(budget);
            }
        });
    }

    @Override
    protected void fetchData(){
    	Log.d(LOG_TAG, "fetchData");
        
    	Log.w(LOG_TAG, "Available Budget : "+db.getNbBudgets());
        
    	
        refreshUI();
    }

    @Override
	protected void refreshUI() {
    	Toast.makeText(BudgetActivity.this, "New budget created", Toast.LENGTH_SHORT);

    	hideWaitingMessage();
	}

    /**
     * 
     * String : arguments type
     * Integer : progress value
     * Bitmap : return type
     */
	private class BackgroundTask extends AsyncTask<Budget, Integer, Boolean> { 

		/** 
		 * Step 1 - invoked on the UI thread immediately after the task is executed. 
		 * This step is normally used to setup the task, for instance by showing a progress bar in the user interface 
		 */
		protected void onPreExecute() { 
	        Log.d(LOG_TAG, "BG 1/3 - onPreExecute");
		} 

		/** 
		 * Step 2 - invoked on the background thread immediately after onPreExecute() finishes executing. 
		 * This step is used to perform background computation that can take a long time 
		 */
		protected Boolean doInBackground(Budget... args) { 
	        Log.d(LOG_TAG, "BG 2/3 - doInBackground");

	        for ( Budget budget : args){
	        	System.out.println(budget);
	        	db.insertBudget(budget);
			}
		
			return true;
		} 

		/** 
		 * Step 3 - invoked on the UI thread after the background computation finishes. 
		 * The result of the background computation is passed to this step as a parameter. 
		 */
		protected void onPostExecute(Boolean  result) {
	        Log.d(LOG_TAG, "BG 3/3 - onPostExecute");

/*	        if ( result ) 
	        	Toast.makeText(BudgetActivity.this, "New budget created", Toast.LENGTH_SHORT);
	        else 
	        	Toast.makeText(BudgetActivity.this, "An issue occured !!!", Toast.LENGTH_SHORT);
	        */refreshUI();
		}
		
		 protected void onProgressUpdate(Integer... progress) {
		        Log.d(LOG_TAG, "BG - onProgressUpdate");
	         //setProgressPercent(progress[0]);
	     }
	}

	
}
